package com.blast.sspai.fragment;

import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import android.widget.TextView;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.Volley;
import com.blast.sspai.R;
import com.blast.sspai.util.RequestManager;

/**
 *首页上方的ViewPager中单个广告的fragment
 */
public class AdvertFragment extends Fragment implements View.OnClickListener {


    private OnFragmentInteractionListener mListener;

    private String title;
    private String imageUrl;

    public static AdvertFragment newInstance(String param1, String param2) {
        AdvertFragment fragment = new AdvertFragment();

        return fragment;
    }

    public AdvertFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_advert, container, false);
        Bundle bundle = getArguments();

        view.setOnClickListener(this);
        title = bundle.getString("title");
        imageUrl = bundle.getString("image");
        Object id = bundle.get("id");
        TextView textView = (TextView) view.findViewById(R.id.advert_fragemet_title);
        textView.setText(title);
       // RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
        NetworkImageView imageView = (NetworkImageView)view.findViewById(R.id.advert_fragment_imageview);
        imageView.setDefaultImageResId(R.drawable.pic_ad_default_banner);
        imageView.setErrorImageResId(R.drawable.pic_ad_default_banner);
        imageView.setImageUrl(imageUrl, RequestManager.getInstance().getImageLoader());

        return view;
    }

    // TODO: Rename method, update argument and hook method into UI event
    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            mListener = (OnFragmentInteractionListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    @Override
    public void onClick(View v) {

    }

    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p/>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        public void onFragmentInteraction(Uri uri);
    }

}
